﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSV.Models;
using Pagination;
using TSV.Library.Common;
using System.Collections;
using TSV.Areas.Administration.Models;
namespace TSV.Controllers
{
    public class CategoryViewController : BaseViewController
    {
        #region Variable
        private CategoryDao catDao = new CategoryDao();
        private PartnerDao partnerDao = new PartnerDao();
        private static int defaultPageSize = Constants.DEFAULT_PAGE_SIZE;

        #endregion

        public ActionResult PromotionInfo(string id)
        {
            if(CheckUtil.IsPositiveNumber(id))
            {
                 Promotion obj = catDao.GetPromotionByID(int.Parse(id));
                if(obj != null)
                {
                    return View(obj);
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult Detail(string id)
        {
            if (CheckUtil.IsPositiveNumber(id))
            {
                string filterText = string.Empty;
                int filterProvinceValue = 0;
                int filterDistricValue = 0;
                int partnerID = int.Parse(id);
                Partner obj = partnerDao.GetPartnerByID(partnerID);
                if (obj != null)
                {
                    string navigation = obj.Category.CategoryName;
                    if (Session[SessionKey.FILTER_CATEGORY] != null)
                    {
                        Hashtable hashData = (Hashtable)Session[SessionKey.FILTER_CATEGORY];
                        filterText = (string)hashData[Constants.FILTER_TEXT];
                        string tempProvinceValue = (string)hashData[Constants.FILTER_PROVINCE_VALUE];
                        filterProvinceValue = !string.IsNullOrEmpty(tempProvinceValue) ? int.Parse(tempProvinceValue) : 0;
                        string tempDistrictValue = (string)hashData[Constants.FILTER_DISTRICT_VALUE];
                        filterDistricValue = !string.IsNullOrEmpty(tempDistrictValue) ? int.Parse(tempDistrictValue) : 0;
                    }
                    ViewData["LinkBack"] = FRouter._("danhmuc", obj.CategoryID.ToString(), obj.Category.CategoryName);
                    ViewData["CategoryKeyWord"] = filterText;
                    ViewData["ProvinceList"] = new SelectList(catDao.GetProvinceList(obj.CategoryID), "ID", "ProvinceDisplay", filterProvinceValue);
                    if (filterProvinceValue > 0)
                    {
                        Province objPronvince = catDao.GetProvinceByID(filterProvinceValue);
                        navigation += " &gt; " + objPronvince.ProvinceName;
                        ViewData["DistrictList"] = new SelectList(catDao.GetDitrictListByProvinceID(filterProvinceValue), "ID", "DistrictName", filterDistricValue);
                    }
                    else
                    {
                        ViewData["DistrictList"] = new SelectList(new List<string>(), "ID", "DistrictName", filterDistricValue);
                    }
                    if (filterDistricValue > 0)
                    {
                        District objDsitrict = catDao.GetDistrictByID(filterDistricValue);
                        navigation += " &gt; " + objDsitrict.DistrictName;
                    }
                    ViewData["NavigationLink"] = navigation;

                    //ViewData["CategoryList"] = catDao.GetList();
                    ViewData["PartnerAddesssList"] = partnerDao.GetPartnerAddressList(partnerID);
                    ViewData["MainPartnerAddesss"] = partnerDao.GetMainPartnerAddress(partnerID);
                    ViewData["PartnerGallery"] = partnerDao.GetPartnerGallery(partnerID);
                    ViewData["PartnerPromotion"] = partnerDao.GetPartnerPromotion(partnerID);
                    ViewData["AdvertisingLeft"] = new AdvertisingDao().GetAdvertisingLeft();
                    return View(obj);
                }
            }
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Index(string id, int? page)
        {
            if (CheckUtil.IsPositiveNumber(id))
            {
                string filterText = string.Empty;
                int filterProvinceValue = 0;
                int filterDistricValue = 0;
                int categoryID = int.Parse(id);
                //int oldCategoryID = 0;
                Category objCategory = catDao.GetByID(categoryID);
                if (objCategory != null)
                {
                    string navigation = "<a href='" + FRouter._("danhmuc", objCategory.ID.ToString(), objCategory.CategoryName) + "'>" + objCategory.CategoryName + "</a>";
                    if (Session[SessionKey.FILTER_CATEGORY] != null)
                    {
                        Hashtable hashData = (Hashtable)Session[SessionKey.FILTER_CATEGORY];
                        filterText = (string)hashData[Constants.FILTER_TEXT];
                        string tempProvinceValue = (string)hashData[Constants.FILTER_PROVINCE_VALUE];
                        filterProvinceValue = !string.IsNullOrEmpty(tempProvinceValue) ? int.Parse(tempProvinceValue) : 0;
                        string tempDistrictValue = (string)hashData[Constants.FILTER_DISTRICT_VALUE];
                        filterDistricValue = !string.IsNullOrEmpty(tempDistrictValue) ? int.Parse(tempDistrictValue) : 0;
                       // string tempCategoryID = (string)hashData[Constants.FILTER_CATEGORY_VALUE];
                       // oldCategoryID = !string.IsNullOrEmpty(tempCategoryID) ? int.Parse(tempCategoryID) : 0;
                    }
                    ViewData["LinkBack"] = FRouter._("danhmuc", objCategory.ID.ToString(), objCategory.CategoryName);
                    ViewData["CategoryKeyWord"] = filterText;
                    ViewData["ProvinceList"] = new SelectList(catDao.GetProvinceList(categoryID), "ID", "ProvinceDisplay", filterProvinceValue);
                    if (filterProvinceValue > 0)
                    {
                        Province objPronvince = catDao.GetProvinceByID(filterProvinceValue);
                        navigation += " &gt; " + objPronvince.ProvinceName;
                        ViewData["DistrictList"] = new SelectList(catDao.GetDitrictListByProvinceID(filterProvinceValue), "ID", "DistrictName", filterDistricValue);
                    }
                    else
                    {
                        ViewData["DistrictList"] = new SelectList(new List<string>(), "ID", "DistrictName", filterDistricValue);
                    }
                    if (filterDistricValue > 0)
                    {
                        District objDsitrict = catDao.GetDistrictByID(filterDistricValue);
                        navigation += " &gt; " + objDsitrict.DistrictName;
                    }
                    ViewData["NavigationLink"] = navigation;
                    List<sp_GetPartnerResult> partnerList = partnerDao.GetPartner(filterText, categoryID, filterProvinceValue, filterDistricValue);
                   // ViewData["CategoryList"] = catDao.GetList();
                    ViewData["PartnerList"] = partnerList;
                    ViewData["AdvertisingLeft"] = new AdvertisingDao().GetAdvertisingLeft();
                    int currentPageIndex = page.HasValue ? page.Value : 1;
                    var list = partnerList.ToPagedList(currentPageIndex, defaultPageSize);
                    if (Request.IsAjaxRequest())
                        return PartialView("List", list);
                    else
                        return View(list);
                }
            }
            return RedirectToAction("Index", "Home");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SetFilter(string text, string valProvince, string valDistrict)
        {
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            result.ContentType = "text/html";
            if (CheckUtil.IsPositiveNumber(valProvince) || CheckUtil.IsPositiveNumber(valDistrict))
            {
                Hashtable hashData = new Hashtable();
                hashData.Add(Constants.FILTER_TEXT, text);
                hashData.Add(Constants.FILTER_PROVINCE_VALUE, valProvince);
                hashData.Add(Constants.FILTER_DISTRICT_VALUE, valDistrict);
                Session[SessionKey.FILTER_CATEGORY] = hashData;
            }
            result.Data = true;
            return result;
        }



    }
}
